﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChatForum.Business.Entity;
using ChatForum.Business.Contract;

namespace ChatForum.Business.Logic
{
    public class AccountLogic
    {
        public static void RegisterMe(User user, IRepository repository)
        {
            IEnumerable<User> allUser = repository.GetAll<User>();
            var commonUserId = allUser.Where(u => u.UserId == user.UserId);
            if (commonUserId != null && commonUserId.Count() > 0)
                throw new DuplicateUserIdException();
            var commonPrimaryEmail = allUser.Where(u => u.EmailId == user.EmailId);
            if (commonPrimaryEmail != null && commonPrimaryEmail.Count() > 0)
                throw new DuplicatePrimaryEmailException();

            user.LastLogInTime = DateTime.Now;
            user.Role = RoleENUM.Member;
            user.Status = true;

            repository.Insert<User>(user);
        }

        public static void LogIn(string userId, string password, IRepository repository)
        {
            var userList = repository.GetAll<User>();

            if (userList == null || userList.Count() == 0)
                throw new InvalidUserException();

            User user = userList.FirstOrDefault(u => u.UserId.Equals(userId, StringComparison.CurrentCultureIgnoreCase));
            if (user == null)
                throw new InvalidUserException();

            user = userList.FirstOrDefault(u => u.UserId.Equals(userId, StringComparison.CurrentCultureIgnoreCase)
                                             && u.Password.Equals(password, StringComparison.CurrentCultureIgnoreCase));
            if (user == null)
                throw new InvalidPasswordException();
			if (user.Status == false)
				throw new BlockedUserException();

            user.IsOnline = true;
            repository.Modify<User>(user);
        }

        public static void ChangeMyPassword(string userId, string oldPassword, string newPassword, IRepository repository)
        {
            var userList = repository.GetAll<User>();
            User user = userList.FirstOrDefault(u => u.UserId.Equals(userId, StringComparison.CurrentCultureIgnoreCase)
                                                  && u.Password.Equals(oldPassword, StringComparison.CurrentCultureIgnoreCase));
            if (user == null)
                throw new InvalidPasswordException();

            user.Password = newPassword;
            repository.Modify<User>(user);
        }

        public static void BlockUser(User user, IRepository repository)
        {
            user.Status = false;
            repository.Modify<User>(user);
        }

		public static void UnblockUser(User user, IRepository repository)
		{
			user.Status = true;
			repository.Modify<User>(user);
		}

        public static User GetUser(string userId, IRepository repository)
        {
            var userList = repository.GetAll<User>();
            return userList.FirstOrDefault(u => u.UserId.Equals(userId, StringComparison.CurrentCultureIgnoreCase));
        }


        public static void ModifyAccount(User user, IRepository repository)
        {
            repository.Modify<User>(user);
        }

        public static void Logout(string userId, IRepository repository)
        {
            User user = GetUser(userId, repository);
            user.IsOnline = false;
            repository.Modify<User>(user);
        }

		public static IEnumerable<User> GetAllUser(IRepository repository)
		{
			return repository.GetAll<User>();
		}
	}
}
